package com.jysz.certificate.web.action.mytask;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.jysz.certificate.action.AbstractBaseAction;
import com.jysz.certificate.persistence.dao.cm.ReleaseDao;
import com.jysz.certificate.persistence.enums.PostType;
import com.jysz.certificate.persistence.enums.ReleaseState;
import com.jysz.certificate.persistence.enums.cs.CheckStockStatus;
import com.jysz.certificate.persistence.model.system.Organization;
import com.jysz.certificate.persistence.model.system.Shop;
import com.jysz.certificate.persistence.model.system.User;
import com.jysz.certificate.service.cm.CertificateService;
import com.jysz.certificate.service.cm.CertificationService;
import com.jysz.certificate.service.cm.ReleaseService;
import com.jysz.certificate.service.cs.CheckStockService;
import com.jysz.certificate.service.cs.TheNetworkService;
import com.jysz.certificate.service.mf.BillService;
import com.jysz.certificate.service.system.FinancialService;
import com.jysz.certificate.service.system.OrganizationService;
import com.jysz.certificate.service.system.ShopService;
import com.jysz.certificate.service.workflow.WorkflowService;
import com.jysz.certificate.utils.CommonUtil;

@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Namespace("/mytask")
public class MyJobAction  extends AbstractBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Resource
	private ReleaseService releaseService;//释放单
	@Resource
	private OrganizationService organizationService;//组织架构
	@Resource
	private ShopService shopService ;//销售店
	@Resource
	private CheckStockService csService;//盘库
	@Resource
	private CertificationService certificationService;//合格证
	@Resource
	private TheNetworkService theNetworkService;//二网审核
	@Resource
	private CertificateService certificateService;//合格证修改审核
	@Resource
	private BillService billService;//发票
	@Resource
	private WorkflowService workflowService;// 流程
	@Resource
	private FinancialService financialService;//金融机构
	@Resource
	private ReleaseDao releaseDao;//
	
	private int[] msg;
	private int autm =0;
	private List<Object> objects;
	
	@Action(INDEX)
	public String index(){
		User user = getUser();
		Set<Shop> shops = this.userSet();
		Long[] shopIDs = this.shopIds(shops);
		
		int he= 0;
		if(shops != null){
			he = shops.size();
		}
		//TODO 这个鬼斧神工有极大的优化空间。。
		theCertifiation(shopIDs);//二网合格证数量
		int ji =0;
		if(user.getPost().equals(PostType.Shop)){//驻店员
//			int release = this.releaseQuantum(shops);//释放数量--驻店员--待释放
			int release = this.releaseCertifiation(shops);//当天释放数--驻店员----------------------------待定
//			int csk = this.checkStockShop(shopIDs);//盘库确认(驻店员)
			int obas = this.openBillAmountShopNO(shops);//未实收发票(店)
			msg = new int[]{release, 0, obas, 0, 0, 0};
			ji = 0;
		}else if(user.getPost().equals(PostType.Manager)){//区域经理
			
			int acs = this.addCheckStock(shops);//需要发起的盘库(区域经理)
//			int acsano = this.addCheckStockNO(shopIDs);//有多个未开始的盘库
			int obasno = this.openBillAmountShopNO(shops);//未实收发票(店)
//			int csm = this.checkStockManager(shopIDs);//盘库确认(区域经理)
			int a = he - acs;
			int release = this.releaseCertifiation(shops);//当天释放数
			msg = new int[]{acs, 0, obasno, 0, a, 1, release};
			ji = 2 ;
		}else if(user.getPost().equals(PostType.Responsible)){//监管科
			
			int releaseApproval = this.releaseApprovalQuantum(shops);//释放数量(审批)--监管科？发证
			int obano = this.openBillAmountNO();//未实收发票(金融机构)
			
			int acfa = this.addCertificateAmount(shops);//补录数量
			int wbwa = this.theNetWorkAmount(shops);//二网申请数量
			
			int uca = this.updateCertificateAmount(shops);//合格证修改审核数量
			
			int release = this.releaseCertifiation(shops);//当天释放数
			msg = new int[]{releaseApproval, obano, acfa, wbwa, uca, 2, release};
			ji =1;
		}
		if(null != msg){
			for(int i=0;i<msg.length-ji;i++){
				if(msg[i]>0){
					autm += 1;
				}
			}
		}
		return INDEX;
	}
	/**
	 * 释放合格证数量
	 * @return
	 */
	public Integer releaseQuantum(Set<Shop> shops){
		int ma = 0;
		if(shops != null && shops.size() > 0 ){
				ma += releaseService.shopRelease(shops, ReleaseState.SUBMITED);
		}
		return ma;
	}
	/**
	 * 释放合格证(审批)数量
	 * @return
	 */
	public Integer releaseApprovalQuantum(Set<Shop> shops){
		int ma = 0;
		if(shops != null && shops.size() > 0 ){
				ma += releaseService.shopRelease(shops, ReleaseState.SUBMITED);
		}
		return ma;
	}
	/**
	 * 获得当天释放数
	 * @param shopSet
	 * @return
	 */
	public int releaseCertifiation(Set<Shop> shopSet){
		int count = 0;
//		Set<Shop> shops = shopSet;
		if(null != shopSet){
//			int a =0;
//			Iterator iterator = shops.iterator();
//			Long[] ids = new Long[shops.size()];
//			while(iterator.hasNext()){
//				a++;
//				Shop shop = (Shop)iterator.next();
//				ids[a-1] = shop.getId();
//			}
			count = certificationService.releaseCertifiation(shopSet);
			
		}
		return count;
	}
	
	/**
	 * 获得二网合格证数量
	 * @param shopIDs
	 * @return
	 */
	public void theCertifiation(Long[] shopIDs){
		if(shopIDs != null && shopIDs.length > 0){
			setObjects(certificationService.thePrewarnings(shopIDs));
		}
	}
	/**
	 * 获得所有店铺ID
	 * @param shopSet
	 * @return
	 */
	public Long[] shopIds(Set<Shop> shopSet){
		Set<Shop> shops = shopSet;
		Long[] ids = null;
		if(null != shops && shops.size()>0){
			int a =0;
			Iterator<Shop> iterator = shops.iterator();
			 ids = new Long[shops.size()];
			while(iterator.hasNext()){
				a++;
				Shop shop = iterator.next();
				ids[a-1] = shop.getId();
			}
		}
		if(ids != null && ids.length != 0)
			return ids;
		
		return null;
	}
	
	/**
	 * 获得当前用户 所有的店
	 */
	public Set<Shop> userSet(){
		Set<Shop> shops = new HashSet<Shop>();
		List<Organization> shopId = organizationService.findShopsByCurrentUser(getUser());
		if(shopId != null){
			for(int i=0;i<shopId.size();i++){
				Organization organization = shopId.get(i);
				if(organization.getObjectId() != null){
					Shop shop = shopService.find(organization.getObjectId());
					if(null != shop){
						shops.add(shop);
					}
				}
				
			}
		}
		if(shops.size()>0){
			return shops;
		}else{
			return null;
		}
		
	}
	/**
	 * 本月还有多少家店未盘库、或未完成盘库
	 * @return
	 */
	public Integer addCheckStock(Set<Shop> havaShops){
		int rus = 0;
		if(havaShops != null && havaShops.size() > 0 ){
			//判断是否有在库的合格证
			List<Shop> shops = certificationService.instockCertification(havaShops);
			if(!CommonUtil.isEmptyList(shops)){
				//需要盘库的 减 已经盘库的=还未盘库的
				rus = shops.size() - csService.noCheckStoc(shops, getUser());
			}
			/*Long[] shIDsLongs= new Long[certifications.size()];
			if(certifications.size()!=0){
				for(int i=0;i<certifications.size();i++){
					Shop certification  = certifications.get(i);
					shIDsLongs[i] = certification.getId();
				}
				ma = csService.noCheckStoc(shIDsLongs, getUser());
			}*/
		}
		return rus;
	}
	/**
	 * 需要发起的盘库(区域经理--未开始)
	 * @return
	 */
	public Integer addCheckStockNO(Long[] shopSet){
		int ma = 0;
		if(shopSet != null && shopSet.length !=0 ){
				ma += csService.noCheckStocOption(shopSet, getUser(), CheckStockStatus.UNSTART, PostType.Manager, null, null);
			}
		return ma;
	}
	/**
	 * 盘库确认(驻店员)
	 * @return
	 */
	/*public Integer checkStockShop(Long[] shopSet){
		int ma = 0;
		if(shopSet != null && shopSet.length !=0 ){
				ma += csService.noCheckStocOption(shopSet, null, CheckStockStatus.SUBMITED, null, getUser(), null);
			}
		return ma;
	}*/
	
	/**
	 * 盘库确认(区域经理)
	 * @return
	 */
	public Integer checkStockManager(Long[] shopSet){
		int ma = 0;
		if(shopSet != null && shopSet.length !=0 ){
				ma += csService.noCheckStocOption(shopSet, null, CheckStockStatus.UNSTART, PostType.Manager, null, getUser());
			}
		return ma;
	}
	/**
	 * 未实收发票(店)
	 * @return
	 */
	public Integer openBillAmountShopNO(Set<Shop> shops){
		int ma = 0;
		if(shops != null && shops.size() > 0 ){
				ma += billService.openBillAmount(shops);
		}
		return ma;
	}
	/**
	 * 未实收发票(金融机构)
	 * @return
	 */
	public Integer openBillAmountNO(){
		return billService.findOpenBillByFinancial();
	}
	/**
	 * 补录数量
	 * @return
	 */
	public Integer addCertificateAmount(Set<Shop> shops){
		int ma = 0;
		if(shops != null && shops.size() > 0 ){
				ma += certificationService.waitverify(shops);
			}
		return ma;
	}
	/**
	 * 二网申请数量
	 * @return
	 */
	public Integer theNetWorkAmount(Set<Shop> shops){
		int ma = 0;
		if(shops != null && shops.size() > 0 ){
				ma += theNetworkService.waitTheNetWork(shops);
			}
		return ma;
	}
	/**
	 * 合格证修改审核数量
	 * @return
	 */
	public Integer updateCertificateAmount(Set<Shop> shops){
		int ma = 0;
		if(shops != null && shops.size() > 0 ){
				ma += certificateService.waitverify(shops);
			}
		return ma;
	}
	public int[] getMsg() {
		return msg;
	}
	public void setMsg(int[] msg) {
		this.msg = msg;
	}
	public int getAutm() {
		return autm;
	}
	public void setAutm(int autm) {
		this.autm = autm;
	}
	public List<Object> getObjects() {
		return objects;
	}
	public void setObjects(List<Object> objects) {
		this.objects = objects;
	}


}
